package com.jyh.flower;

import java.util.Scanner;

/*
     鲜花销售系统首页
 */
public class FlowerIndex {
    //准备一个键盘输入对象
    public static final Scanner INPUT = new Scanner(System.in);
    //准备一个存订单对象的数组
    public static final FlowerOrder[] orderList = new FlowerOrder[5];

    //让数据一开始就存入数组中
    static {
        orderList[0] = new FlowerOrder("1001", "金色海岸-香槟玫瑰", 10, 318.0, "2016-12-12", "王二昵", "");
        orderList[1] = new FlowerOrder("1002", "初心永不变-白玫瑰", 99, 538.0, "2016-12-20", "张欣雨", "");
        orderList[2] = new FlowerOrder("1003", "梦里花开-紫色桔梗", 40, 148.0, "2016-12-24", "王二昵", "眼波如水，映出窗外繁星点点");
        orderList[3] = new FlowerOrder("1004", "心花怒放-粉香百合", 120, 188.0, "2016-12-26", "王二昵", "内层银灰色厚绵纸，外层深蓝色条纹纸，银灰色缎带");
        orderList[4] = new FlowerOrder("1005", "致青春-无声的爱", 1000, 219.0, "2016-12-18", "张欣雨", "我爱你，胜过爱爱情，更胜过爱自己！");
    }

    public static void main(String[] args) {
        outer:
        while (true) {
            System.out.println("====================欢迎光临\"七彩鲜花\"销售管理系统===================");
            System.out.println("1.查询销售订单");
            System.out.println("2.修改销售订单");
            System.out.println("3.删除销售订单");
            System.out.println("4.系统退出");
            System.out.println("==================================================================");

            //键盘输入一个整数：choice   代表4个功能
            System.out.println("请输出需要进行的选项：");
            int choice = INPUT.nextInt();

            //判断选择哪个功能
            switch (choice) {
                case 1:
                    //1.查询销售订单 findAll();
                    findAll();
                    break;
                case 2:
                    //2.修改销售订单
                    update();
                    break;
                case 3:
                    //3.删除销售订单
                default:
                    //4.系统退出
                    System.out.println("退出系统");
                    break outer;
            }

        }

    }

    //定义查询订单的方法
    public static void findAll() {
        //1.打印表头标题
        System.out.println("编号\t\t\t鲜花名称\t\t销售数量\t\t价格\t\t销售日期\t\t销售员\t\t备注");
        //2.打印查询到的数据
        for (FlowerOrder flowerOrder : orderList) {
            System.out.println(flowerOrder.getId() + "\t" + flowerOrder.getFlowerName() + "\t" + flowerOrder.getSaleNum() + "\t\t\t" + flowerOrder.getPrice() + "\t" + flowerOrder.getDate() + "\t" + flowerOrder.getSaleName() + "\t" + flowerOrder.getBeiZhu());
            System.out.println();
        }
    }

    //修改订单的功能
    public static void update() {
        System.out.println("请输入销售编号：");
        String saleId = INPUT.next();
        //拿到saleNum去数组中遍历查找有没有对应的销售编号对象，没有就提示
        //假设没有
        boolean flag = false;
        //遍历数组
        for (FlowerOrder order : orderList) {
            if (order.getId().equals(saleId)) {
                flag = true;
                break;
            }
        }
        if (flag == false) {
            System.err.println("您查找的订单编号不存在!");
            return;
        }
        //如果有
        System.out.println("您要修改的订单详细信息如下：");
        //输出表头标题信息
        System.out.println("编号\t鲜花名称\t销售数量\t价格\t销售日期\t销售员\t备注");
        //遍历数组，如果编号相同便输出信息
        for (FlowerOrder order : orderList) {
            if (order.getId().equals(saleId)) {
                System.out.println(order.getId() + "\t" + order.getFlowerName() + "\t" + order.getSaleNum() + "\t" + order.getPrice() + "\t" + order.getDate() + "\t" + order.getSaleName() + "\t" + order.getBeiZhu());
            }
        }
        //录入新的修改后的信息
        System.out.println("请输入修改后的鲜花名称");
        String newName = INPUT.next();
        System.out.println("请输入修改后的销售数量");
        int newSaleNum = INPUT.nextInt();
        System.out.println("请输入修改后的鲜花价格");
        double newPrice = INPUT.nextDouble();
        System.out.println("请输入修改后的销售日期：（yyyy-MM-dd）");
        String newDate = INPUT.next();
        System.out.println("请输入修改后的销售员");
        String newSaleName = INPUT.next();
        System.out.println("请输入修改后的备注");
        String newBeiZhu = INPUT.next();

        //封装对象
        FlowerOrder flowerOrder = new FlowerOrder(saleId, newName, newSaleNum, newPrice, newDate, newSaleName, newBeiZhu);
        System.out.println("您确定要修改吗？(y/n)");
        String config = INPUT.next();
        if ("y".equals(config)) {
            // 将 flowerOrder 替换数组中id为uid的对象元素
            for (int i = 0; i < orderList.length; i++) {
                if (saleId.equals(orderList[i].getId())) {
                    orderList[i] = flowerOrder;
                    break;
                }
            }
            System.out.println("修改成功");
        }
        //再调用查询的方法赋新的值
        findAll();

    }
}
